package dsk.demo.product.controller.product;

import dsk.demo.common.utils.R;
import dsk.demo.product.entity.CatalogEntity;
import dsk.demo.product.service.CatalogService;
import dsk.demo.product.service.ProductService;
import dsk.demo.product.vo.CatalogTreeVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


/**
 * @author daishikun
 * @email 940798525@qq.com
 * @date 2022-09-17 09:09:41
 */
@RestController
@RequestMapping("product/catalog")
public class CatalogController {
    @Autowired
    private CatalogService catalogService;

    /**
     * 列表
     */
    @RequestMapping("/list/tree")
    @RequiresPermissions("product:catalog:list")
    public R list() {
        CatalogTreeVo page = catalogService.queryCatalogTree();
        return R.ok().put("data", page.getChildren());
    }

    /**
     * 查询所有子节点
     */
    @RequestMapping("/list/childList")
    @RequiresPermissions("product:catalog:list")
    public R childList(String name) {
        List<CatalogEntity> entities = catalogService.selectAllChildNode(name);
        return R.ok().put("data", entities);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("product:catalog:info")
    public R info(@PathVariable("id") Integer id) {
        CatalogEntity catalog = catalogService.getById(id);
        return R.ok().put("catalog", catalog);
    }

    /**
     * 保存,并清除  目录树的缓存
     */
    @RequestMapping("/save")
    @RequiresPermissions("product:catalog:save")
    @Caching(evict = {
            @CacheEvict(value = "manager", key = "'catalogTree'"),
            @CacheEvict(value = "manager", key = "'catalogNodes'")
    })
    public R save(@RequestBody CatalogEntity catalog) {
        catalog.setStatus(0);
        catalogService.save(catalog);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:catalog:update")
    @Caching(evict = {
            @CacheEvict(value = "manager", key = "'catalogTree'"),
            @CacheEvict(value = "manager", key = "'catalogNodes'")
    })
    public R update(@RequestBody CatalogEntity catalog) {
        catalogService.updateById(catalog);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:catalog:delete")
    @Caching(evict = {
            @CacheEvict(value = "manager", key = "'catalogTree'"),
            @CacheEvict(value = "manager", key = "'catalogNodes'")
    })
    public R delete(@RequestBody Integer[] ids) {
        List<CatalogEntity> entities = new ArrayList<>();
        for (Integer id : ids) {
            CatalogEntity e = new CatalogEntity();
            e.setId(id);
            e.setStatus(1);
            entities.add(e);
        }
        catalogService.updateBatchById(entities);
        return R.ok();
    }

    @Autowired
    private ProductService productService;

}
